package com.zx.mes.hyl.auth.rest;

import com.zx.mes.hyl.auth.pagemodel.Resource;
import com.zx.mes.hyl.auth.service.ResourceServiceI;
import com.zx.mes.hyl.common.msg.ObjectRestResponse;
import com.zx.mes.hyl.common.msg.TableResultResponse;
import com.zx.mes.hyl.common.pagemodel.LayPageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

/**
 * @author hyl
 * @date 2017-12-20
 */
@RestController
@RequestMapping("/resource")
public class ResouceController {

    @Autowired
    private ResourceServiceI resourceService;

    @GetMapping("/")
    public TableResultResponse<Resource> list(Resource resource,LayPageHelper ph){
        return new TableResultResponse<>(resourceService.count(),resourceService.list(resource,ph));
    }

    @GetMapping("/{id}")
    public ObjectRestResponse<Resource> findById(@PathVariable("id") String id){
        ObjectRestResponse<Resource> objectRestResponse=new ObjectRestResponse<>();
        Resource c=resourceService.getById(id);
        if (null==c){
            objectRestResponse.setRel(false);
            objectRestResponse.setData(null);
        }else{
            objectRestResponse.setRel(true);
            objectRestResponse.setData(c);
        }
        return objectRestResponse;
    }

    @PostMapping("/")
    public ObjectRestResponse<String> add(Resource resource) {
        ObjectRestResponse<String> objectRestResponse = new ObjectRestResponse<>();
        try {
            resource.setId(UUID.randomUUID().toString());
            resourceService.add(resource);
            objectRestResponse.setRel(true);
            objectRestResponse.setData("增加成功!");
        } catch (Exception e) {
            objectRestResponse.setRel(false);
            objectRestResponse.setStatus(500);
            objectRestResponse.setData("增加失败!原因 : " + e.getMessage());
        }
        return objectRestResponse;
    }


    @PutMapping("/")
    public ObjectRestResponse<String> update(Resource resource) {
        ObjectRestResponse<String> objectRestResponse = new ObjectRestResponse<>();
        try {
            resourceService.add(resource);
            objectRestResponse.setRel(true);
            objectRestResponse.setData("修改成功!");
        } catch (Exception e) {
            objectRestResponse.setRel(false);
            objectRestResponse.setStatus(500);
            objectRestResponse.setData("修改失败!原因 : " + e.getMessage());
        }
        return objectRestResponse;
    }

    @DeleteMapping("/{id}")
    public ObjectRestResponse<String> delete(@PathVariable("id")String id) {
        ObjectRestResponse<String> objectRestResponse = new ObjectRestResponse<>();
        try {
            resourceService.delete(id);
            objectRestResponse.setRel(true);
            objectRestResponse.setData("删除成功!");
        } catch (Exception e) {
            objectRestResponse.setRel(false);
            objectRestResponse.setStatus(500);
            objectRestResponse.setData("删除失败!原因 : " + e.getMessage());
        }
        return objectRestResponse;
    }

}
